package com.ceshiren.hogwarts.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class TestWeb {
//    private static ObjectMapper mapper;
//      private static WebDriver webDriver = new ChromeDriver();

    /*@Test
    public void startSelenium(){
        WebDriver wd = new ChromeDriver();
        wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        wd.get("https://ceshiren.com/");
        wd.findElement(By.cssSelector("#search-button")).click();
        wd.findElement(By.cssSelector("#search-term")).sendKeys("selenium");
    }*/
    @Test
    public void testGetCookie() throws IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        webDriver.get("https://work.weixin.qq.com/wework_admin/frame");
        Set<Cookie> cookies = webDriver.manage().getCookies();
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        mapper.writeValue(new File("cookies.yaml"),cookies);
    }

    @Test
    public void testLogin() throws IOException {
        WebDriver webDriver = new ChromeDriver();
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        webDriver.get("https://work.weixin.qq.com/wework_admin/frame");
      /*  TypeReference<List<HashMap<String, Object>>> typeReference = new TypeReference<List<HashMap<String, Object>>>() {
        };*/
        TypeReference<List<HashMap<String, Object>>> typeReference = new TypeReference<List<HashMap<String, Object>>>() {
        };
        List<HashMap<String, Object>> cookies = mapper.readValue(new File("cookies.yaml"), typeReference);
        System.out.println(cookies);
        cookies.forEach(cookieMap -> webDriver.manage().addCookie(new Cookie(cookieMap.get("name").toString(), cookieMap.get("value").toString())));
        webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        webDriver.navigate().refresh();
    }

}
